﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApp7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtFile1.Text = openFileDialog.FileName;
                }
            }
        }

        private void btnBrowse2_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtFile2.Text = openFileDialog.FileName;
                }
            }
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtFile1.Text) || string.IsNullOrWhiteSpace(txtFile2.Text))
            {
                MessageBox.Show("请先选择两个文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                string dataDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
                if (!Directory.Exists(dataDir))
                {
                    Directory.CreateDirectory(dataDir);
                }

                // 创建合并后的文件名
                string mergedFileName = $"merged_{DateTime.Now:yyyyMMddHHmmss}.txt";
                string mergedFilePath = Path.Combine(dataDir, mergedFileName);

                // 读取并合并文件内容
                string content1 = File.ReadAllText(txtFile1.Text);
                string content2 = File.ReadAllText(txtFile2.Text);
                string mergedContent = content1 + Environment.NewLine + content2;

                File.WriteAllText(mergedFilePath, mergedContent);

                lblStatus.Text = $"文件已成功合并到: {mergedFilePath}";
                MessageBox.Show($"文件合并成功!\n保存路径: {mergedFilePath}", "成功",
                              MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                lblStatus.Text = "文件合并失败";
                MessageBox.Show($"合并文件时出错: {ex.Message}");
            }
        }
    }
}
